import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/closure_appointment_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/task_closure/common/common_closure_method.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/task_closure/task_appointment_review/common_approval_list/common_approval_list_controller.dart';

/// 通用审批列表
class CommonApprovalListPage extends StatelessWidget {
  CommonApprovalListPage({required this.type, required this.globalKey, super.key});

  // 预约类型 0：人员 1：普通车辆 2：危化车辆
  final int type;
  final GlobalKey<CustomEasyRefreshListState> globalKey;
  final controller = Get.put(CommonApprovalListController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        initState: (state) {
          controller.initData(globalKey);
        },
        builder: (_) {
          return Container(
            color: const Color(0xffF5F5F6),
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.w),
            child: Column(
              children: [
                _getTimeWidget(),
                AppGaps.vGap10,
                Expanded(
                  child: CustomEasyRefreshList<ClosureAppointmentEntity>(
                    key: globalKey,
                    isShowSearch: true,
                    searchHint: type == 0 ? '请输入姓名' : '请输入车牌号',
                    dataLoad: (pageIndex, pageSize, {String? search}) async {
                      return controller.getListData(pageIndex, pageSize, search: search, type: type);
                    },
                    itemBuilder: (ClosureAppointmentEntity item, int index) {
                      switch (type) {
                        case 0:
                          return _getPersonnel(item);
                        case 1:
                        case 2:
                          return _getVehicle(item);
                        default:
                          return Container();
                      }
                    },
                  ),
                ),
              ],
            ),
          );
        });
  }

  /// 获取当前类型时间选择控件
  /// [type] 0：人员 1：普通车辆 2：危化车辆
  Widget _getTimeWidget() {
    switch (type) {
      case 0:
        return CustomSelectTimePeriod(
          startTime: controller.personnelStartTime,
          endTime: controller.personnelEndTime,
          onConfirm: (start, end) {
            controller.selectTimePeriod(start, end, type);
          },
          iconCalendar: AppImages.icon_course_clock,
        );
      case 1:
        return CustomSelectTimePeriod(
          startTime: controller.vehicleStartTime,
          endTime: controller.vehicleEndTime,
          onConfirm: (start, end) {
            controller.selectTimePeriod(start, end, type);
          },
          iconCalendar: AppImages.icon_course_clock,
        );
      case 2:
        return CustomSelectTimePeriod(
          startTime: controller.dangerousStartTime,
          endTime: controller.dangerousEndTime,
          onConfirm: (start, end) {
            controller.selectTimePeriod(start, end, type);
          },
          iconCalendar: AppImages.icon_course_clock,
        );
      default:
        return Container();
    }
  }

  /// 人员类型卡片
  Widget _getPersonnel(ClosureAppointmentEntity item) {
    return CustomTitleChartPage(
      title: item.name,
      titleRightWidget: _getStateView(item),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          CustomTextWidget.getTextRowView(title: '人员类型:', value: CommonClosureMethod.getPersonTypeValue(item.type)),
          AppGaps.vGap12,
          CustomTextWidget.getTextRowView(title: '所属单位：', value: item.affiliatedUnit),
          AppGaps.vGap12,
          CustomTextWidget.getTextRowView(title: '预计来访时间：', value: item.startDate),
          Visibility(
            visible: item.appointmentStatus == 0,
            child: Column(
              children: [
                AppGaps.vGap12,
                _getApprovalButton(),
              ],
            ),
          ),
        ],
      ),
    ).clickOnce(() {
      controller.toApprovalDetailsPage(type: type, item: item);
    });
  }

  /// 车辆类型卡片
  Widget _getVehicle(ClosureAppointmentEntity item) {
    return CustomTitleChartPage(
      title: item.licensePlate,
      titleRightWidget: _getStateView(item),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Visibility(
            visible: type == 1,
            child: Column(
              children: [
                CustomTextWidget.getTextRowView(title: '车辆类型:', value: CommonClosureMethod.getCarTypeValue(item.carType)),
                AppGaps.vGap12,
              ],
            ),
          ),
          CustomTextWidget.getTextRowView(title: '驾驶员姓名：', value: item.name),
          AppGaps.vGap12,
          CustomTextWidget.getTextRowView(title: '预计来访时间：', value: item.startDate),
          Visibility(
            visible: item.appointmentStatus == 0,
            child: Column(
              children: [
                AppGaps.vGap12,
                _getApprovalButton(),
              ],
            ),
          ),
        ],
      ),
    ).clickOnce(() {
      controller.toApprovalDetailsPage(type: type, item: item);
    });
  }

  /// 获取状态按钮
  Widget _getStateView(ClosureAppointmentEntity item) {
    String? stateText;
    Color? color;
    switch (item.appointmentStatus) {
      case 0:
        stateText = '待审批';
        color = const Color(0xffFC9900);
        break;
      case 1:
        stateText = '通过';
        color = const Color(0xFF0FCE0F);
        break;
      case 2:
        stateText = '不通过';
        color = const Color(0xFFFF2C2C);
        break;
    }
    return Container(
      alignment: Alignment.center,
      height: 18.w,
      width: 52.w,
      decoration: BoxDecoration(
        color: color,
        // color: const Color(0xffFC9900),
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20.w),
          topLeft: Radius.circular(20.w),
          bottomRight: Radius.circular(20.w),
        ),
      ),
      child: Text(
        stateText ?? '',
        style: TextStyle(
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  /// 获取操作按钮
  Widget _getApprovalButton() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          '操作:',
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(10.w, 6.w, 6.w, 6.w),
          decoration: BoxDecoration(
            color: const Color(0xFFE5E7FF),
            borderRadius: BorderRadius.circular(4.w),
          ),
          child: Row(
            children: [
              Text(
                '审核',
                style: TextStyle(
                  fontSize: 13.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff5C6DFF),
                ),
              ),
              AppGaps.hGap2,
              SvgPicture.asset(
                AppImages.icon_right_arrow,
              ),
            ],
          ),
        ),
      ],
    );
  }
}
